package com.tes.dao;

import java.util.ArrayList;
import java.util.List;

import com.tes.database.DatabaseHelper;
import com.tes.model.Setting;
import com.tes.tabel.TabelSetting;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SettingDAO {
	private Context context;

	public SettingDAO(Context context) {
		this.context = context;
	}

	public List<Setting> gets() {
		List<Setting> result = new ArrayList<Setting>();

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			String kolom[] = new String[] { TabelSetting.KOLOM_ID,
					TabelSetting.KOLOM_VALUE };
			Cursor c = db.query(TabelSetting.NAMA_TABEL, kolom, null, null,
					null, null, null);

			while (c.moveToNext()) {
				Setting s = new Setting();

				s.setId_setting(c.getString(0));
				s.setValue(c.getInt(1) == 1 ? true : false);

				result.add(s);
			}

			c.close();
			db.close();
		} catch (Exception e) {
			result = null;
		}

		return result;
	}

	public Setting get(String id) {
		Setting result = new Setting();

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			String kolom[] = new String[] { TabelSetting.KOLOM_ID,
					TabelSetting.KOLOM_VALUE };
			String selection = TabelSetting.KOLOM_ID + " = " + id;
			Cursor c = db.query(TabelSetting.NAMA_TABEL, kolom, selection,
					null, null, null, null);

			if (c.moveToFirst()) {
				Setting s = new Setting();

				s.setId_setting(c.getString(0));
				s.setValue(c.getInt(1) == 1 ? true : false);

				result = s;
			}

			c.close();
			db.close();
		} catch (Exception e) {
			result = null;
		}

		return result;
	}

	public boolean insertSetting(Setting setting) {
		boolean result = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();
			ContentValues cv = new ContentValues();

			cv.put(TabelSetting.KOLOM_ID, setting.getId_setting());
			cv.put(TabelSetting.KOLOM_VALUE, setting.getValue());

			result = db.insert(TabelSetting.NAMA_TABEL, null, cv) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			result = (Boolean) null;
		}

		return result;
	}

	public boolean updateSetting(Setting setting) {
		boolean result = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();
			ContentValues cv = new ContentValues();

			cv.put(TabelSetting.KOLOM_VALUE, setting.getValue());

			String whereClause = TabelSetting.KOLOM_ID + " = "
					+ setting.getId_setting();
			result = db.update(TabelSetting.NAMA_TABEL, cv, whereClause, null) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			result = (Boolean) null;
		}

		return result;
	}

	public boolean deleteSetting(String id) {
		boolean hasil = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			String whereClause = TabelSetting.KOLOM_ID + " = " + id;
			hasil = db.delete(TabelSetting.NAMA_TABEL, whereClause, null) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			hasil = (Boolean) null;
		}

		return hasil;
	}

	public boolean deleteAll() {
		boolean hasil = false;

		try {
			DatabaseHelper dh = new DatabaseHelper(context);
			SQLiteDatabase db = dh.getReadableDatabase();

			hasil = db.delete(TabelSetting.NAMA_TABEL, null, null) > 0 ? true
					: false;

			db.close();
		} catch (Exception e) {
			hasil = (Boolean) null;
		}

		return hasil;
	}
}
